

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionbeans.AdminBeanRemote;

/**
 * Servlet implementation class AbilityAddingServlet
 */
public class AbilityAddingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AbilityAddingServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ability,result;
		InitialContext context;
		
		try {
			context = new InitialContext();
			Object ref = context.lookup("AdminBeanRemote");
			AdminBeanRemote adminBean = (AdminBeanRemote)PortableRemoteObject.narrow(ref, AdminBeanRemote.class);
			
			ability = request.getParameter("ability");
			if (ability != null) {
				result = adminBean.addAbility(ability);
				if (result.equals("already existing ability")) {
					request.getSession().setAttribute("status", "already existing ability");
					response.sendRedirect("AbilityAddingPage.jsp");
				}
				else if (result.equals("bad ability name")) {
					request.getSession().setAttribute("status", "bad ability name");
					response.sendRedirect("AbilityAddingPage.jsp");
				}
				else {
					request.getSession().setAttribute("status", "ability added successfully");
					response.sendRedirect("AbilityAddingPage.jsp");
				}
			}
		} catch (NamingException e) {
			System.out.println("Naming Exception");
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
